package com.compass.doctor.ui.consultation

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.databinding.ActivityConsultationCenterBinding
import com.compass.doctor.ui.chat.util.CustomUIConfig.initMeetingKitConfig
import com.compass.doctor.ui.consultation.fragment.ConsultationFragment
import com.compass.doctor.ui.consultation.viewmodel.ConsultationViewModel
import com.compass.framework.adapter.ViewPage2FragmentAdapter
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.ext.onClick
import com.compass.framework.utils.LogUtil
import com.netease.yunxin.kit.meeting.sdk.NECallback
import com.netease.yunxin.kit.meeting.sdk.NEMeetingError
import com.netease.yunxin.kit.meeting.sdk.NEMeetingKit

/**
 * Description： 会诊中心
 * Date:2023/12/21 15:28
 */
class ConsultationCenterActivity : BaseVBActivity<ActivityConsultationCenterBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, ConsultationCenterActivity::class.java)
            context.startActivity(intent)
        }
    }


    private lateinit var viewPageAdapter: ViewPage2FragmentAdapter
    private var fragments: ArrayList<Fragment> = ArrayList()
    private lateinit var foundingFragment: ConsultationFragment//发起的
    private lateinit var initiateFragment: ConsultationFragment //加入的

    override fun initView(savedInstanceState: Bundle?) {
        initFragment()
        mBinding.dslTab.tabIndicator.indicatorWidth = -2
        mBinding.dslTab.tabIndicator.indicatorEnableFlow = true
        mBinding.dslTab.configTabLayoutConfig {
            onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                val toIndex = selectIndexList.first()
                mBinding.viewpager2.setCurrentItem(toIndex, false)

            }
        }
        viewPageAdapter = ViewPage2FragmentAdapter(supportFragmentManager, lifecycle, fragments)
        mBinding.viewpager2.adapter = viewPageAdapter
        mBinding.viewpager2.isUserInputEnabled = false//禁止viewPage滑动
        mBinding.viewpager2.setCurrentItem(0, false)
        mBinding.tvSubmit.onClick {
            if (NEMeetingKit.getInstance().isInitialized){
                CreateConsultationActivity.start(this)
            }else{
                showToast("正在初始化会诊模块")
            }

        }
    }

    private fun initFragment() {
        foundingFragment = ConsultationFragment()
        val bundleReception = Bundle()
        bundleReception.putInt(KEY_ORDER_TYPE, 1)
        foundingFragment.arguments = bundleReception
        fragments.add(foundingFragment)

        initiateFragment = ConsultationFragment()
        val bundleAlready = Bundle()
        bundleAlready.putInt(KEY_ORDER_TYPE, 2)
        initiateFragment.arguments = bundleAlready
        fragments.add(initiateFragment)
    }

    override fun initData() {
        if (!NEMeetingKit.getInstance().isInitialized){
            initMeetingKitConfig(NECallback<Void> { resultCode, resultMsg, _ ->
                if (resultCode == NEMeetingError.ERROR_CODE_SUCCESS) {
                    //初始化成功
                    LogUtil.e("初始化成功==$resultMsg")

                } else {
                    showToast(resultMsg)
                }
            })
        }
    }
}